package com.vpclub;

import com.vpclub.bean.Yellow;
import com.vpclub.config.MainConfigOfProfile;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;
import java.util.Arrays;

/**
 * @author : leimingchao
 * @date Date : 2021年06月22日 21:44
 */
public class IOCTest_Profile {


    /**
     * 1.使用命令行参数:在虚拟机参数位置加载：-Dspring.profiles.active=test
     * 2.代码的方式激活某种环境
     */
    @Test
    public void test() {
        //方式一：
        // AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
        // Arrays.stream(applicationContext.getBeanNamesForType(DataSource.class))
        //         .forEach(System.out::println);
        //方式二：
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        Arrays.stream(applicationContext.getEnvironment().getActiveProfiles()).forEach(System.out::println);
        //1.创建一个applicationContext对象
        //2.设置需要激活的环境
        applicationContext.getEnvironment().setActiveProfiles("test");
        Arrays.stream(applicationContext.getEnvironment().getActiveProfiles()).forEach(System.out::println);
        //3.注册主配置类
        applicationContext.register(MainConfigOfProfile.class);
        //4.启动刷新容器
        applicationContext.refresh();
        Arrays.stream(applicationContext.getBeanNamesForType(DataSource.class))
                .forEach(System.out::println);

        Yellow yellow = applicationContext.getBean(Yellow.class);
        System.out.println(yellow);
        applicationContext.close();
    }
}
